package com.clss.emergencycall.tools.eventbus;

import android.text.TextUtils;

import com.clss.emergencycall.base.Constant;
import com.clss.emergencycall.bean.BaseJson;
import com.clss.emergencycall.bean.ChatMsgTextEntity;
import com.clss.emergencycall.bean.EmergencyLocation;
import com.clss.emergencycall.utils.Lg;
import com.clss.emergencycall.utils.SpHelper;
import com.google.gson.JsonObject;
import com.clss.emergencycall.bean.StompMessageEntity;

import org.greenrobot.eventbus.EventBus;
import java.util.UUID;

/**
 * 发起EventBus信息
 *
 * @author LiuV
 * @date 2017/6/13
 */

public class EventBusHelper {

    private static final String TAG = "EventBusHelper";
    //返回的BaseJson为空
    public static final int RESPONSE_NULL = -11;
    //登录成功
    public static final int LOGIN_SUCCESS = 66;
    //登录状态异常，token失效
    public static final int LOGIN_STATUS_ERROR = -99;
    //模擬報警視頻結束
    public static final int VIDEO_CALL_FINISH = 70;
    //更新个人信息成功
    public static final int UPDATE_MY_INFO_SUCCESS = 71;
    //数据库有相同数据刷新首页菜单
    public static final int UPDATE_HOME_SERVICE = 72;
    //沒有更多數據
    public static final int NO_MORE_DATA = 2004;
    //定位成功
    public static final int LOCATION_SUCCESS = 661;
    //有未读消息
    public static final int HAS_NEW_MESSAGE_UNREAD = 662;
    //有警务服务
    public static final int HAS_POLICE_SERVER_LIST = 663;
    //连接断开
    public static final int JANUS_HANG_UP = 664;
    //成功拿到了当前位置的host
    public static final int CHECK_HOST_SUCCESS = 67;
    //需要实名认证
    public static final int MUST_AUTH = 1021;
    //发送stomp消息
    public static final int SEND_MESSAGE_CODE = 670;
    //收到stomp消息
    public static final int RECEIVE_MESSAGE_CODE = 671;
    //取消订阅
    public static final int UNSUBSCRIBE_MESSAGE_CODE = 672;
    //开启stomp中收到消息的提示
    public static final int ENABLE_NEW_MESSAGE_NOTIFY = 673;
    //关闭stomp中收到消息的提示
    public static final int DISABLE_NEW_MESSAGE_NOTIFY = 674;
    //订阅某个topic
    public static final int SUBSCRIBE_MESSAGE_CODE = 675;
    //通知stomp开始坐标上传
    public static final int STOMP_START_POINT_UPLOAD = 676;
    //通知stomp停止坐标上传
    public static final int STOMP_END_POINT_UPLOAD = 677;
    /** 警察可能要撩你 */
    public static final int HAS_NEW_TEXT_CALL = 678;
    /** 已经更新了topicId列表 */
    public static final int HAS_LAST_CASEID_LIST = 679;
    /** 停止service长连接 */
    public static final int STOP_STOMP_KEEP_ALIVE = 682;
    /*** 发送给警民沟通页面  移动地图marker */
    public static final int SEND_POLICE_ABOAT_CHANGE_LOCATION = 680;
    /** 收到一个报警结束，刷新首页黄条 */
    public static final int HAS_POLICE_END_UPDATA_YELLOW = 683;
    /** 需要订阅一个房间 */
    public static final int NEED_TOPIC_ROOM_FRIEND = 684;
    /** 没次发送消息后（图片 视频等需要上传）回调到发送页面（取消掉加载框） */
    public static final int SEND_LOCAL_SUCCESS_RESOURSE = 685;
    /** 发送消息失败后（图片 视频等需要上传）回调到发送页面（取消掉加载框） */
    public static final int SEND_LOCAL_FAIL_RESOURSE = 691;
    /** 报警结束，关闭页面 */
    public static final int ALARM_END_CLOSE_A = 690;
    /** 接警台未接超时 */
    public static final int ALARM_BUSY_CLOSE_A = 692;
    /** 接警台取消回拨 */
    public static final int EVENT_CANCEL_VIDEO_CALL = 693;
    /** 报警提示文字(websocket)  -- 改为接通通知 */
    public static final int EVENT_ALARM_TIPS_NOTICE = 695;
    /** 接警台处警通知(websocket) */
    public static final int EVENT_ALARM_CASE_HANDLE_NOTICE = 698;
    /** 接警台名称修改 */
    public static final int EVENT_ALARM_CGANGE_NAME = 699;

    /** 有可用网络 */
    public static final int NETWORK_IS_CONNECTED = 799;
    public static final int WECHAT_LOGIN = 800;


    /** 122警情有新进度的通知 */
    public static final int TRAFFIC_HAS_NEW_PROGRESS = 801;

    public static final String JANUS_HANGUP_ICE_FAILED = "ICE failed";

    public static void sendEventBusMsg(int code, String msg) {
        EventBus.getDefault().post(new EventBusMessage(code, msg));
    }

    public static void sendEventBusMsg(int code, String msg, String msg2) {
        EventBus.getDefault().post(new EventBusMessage(code, msg,msg2));
    }

    public static void sendEventBusMsg(int code, String msg, long time, int type) {
        EventBus.getDefault().post(new EventBusMessage(code, msg, time, type));
    }

    public static void sendEventBusMsg(int code) {
        EventBus.getDefault().post(new EventBusMessage(code));
    }

    public static void sendEventBusMsg(String msg) {
        EventBus.getDefault().post(new EventBusMessage(msg));
    }

    public static void sendStompMsg(String stompId, ChatMsgTextEntity chatMsgTextEntity) {
        EventBus.getDefault().post(new EventBusMessage(SEND_MESSAGE_CODE, stompId, chatMsgTextEntity));
    }

    public static void receiveStompMsg(String stompId, ChatMsgTextEntity chatMsgTextEntity) {
        EventBus.getDefault().post(new EventBusMessage(RECEIVE_MESSAGE_CODE, stompId, chatMsgTextEntity));
    }

    public static void sendStompMsg(StompMessageEntity stompMessageEntity) {
        EventBus.getDefault().post(new EventBusMessage(SEND_MESSAGE_CODE, stompMessageEntity));
    }

    public static void receiveStompMsg(StompMessageEntity stompMessageEntity){
        EventBus.getDefault().post(new EventBusMessage(RECEIVE_MESSAGE_CODE, stompMessageEntity));
    }

    public static void sendEventBusMessage(int code,StompMessageEntity stompMessageEntity){
        EventBus.getDefault().post(new EventBusMessage(code, stompMessageEntity));
    }

    public static void sendEventBusMessage(EventBusMessage eventBusMessage){
        EventBus.getDefault().post(eventBusMessage);
    }

    public static void handleBaseJson(BaseJson baseJson) {
//        Lg.i(TAG, "---handleBaseJson===" + baseJson.code);
        if (baseJson != null) {
            if (baseJson.getCode() == 0 || baseJson.getCode() == 201) {

            } else {
                sendEventBusMsg(baseJson.getCode(), baseJson.getError());
            }
        } else {
            sendEventBusMsg(RESPONSE_NULL, "");
        }
    }

    public static void sendLocationInfo() {
        EmergencyLocation location = SpHelper.getInstance().getLocation();
        Lg.i(TAG, "---sendLocationInfo===");
        if (TextUtils.isEmpty(location.getLat())){
            return;
        }
        JsonObject jsonObject = new JsonObject();
        String adcode = SpHelper.getInstance().getString(SpHelper.AMAPLOCATION_INFO_AREA);
        jsonObject.addProperty(Constant.lat, location.getLat());
        jsonObject.addProperty(Constant.lng, location.getLng());
        jsonObject.addProperty(Constant.address, location.getAddress());
        jsonObject.addProperty(Constant.adCode,adcode);
        jsonObject.addProperty(Constant.province, location.getProvince());
        jsonObject.addProperty(Constant.city, location.getCity());
        jsonObject.addProperty(Constant.district, location.getArea());
        jsonObject.addProperty(Constant.street, location.getStreet());
        StompMessageEntity stompMessageEntity = createStompMessage(Constant.S_UPLOAD_USER_COORDINATE);
        stompMessageEntity.setBody(jsonObject);
        Lg.i(TAG, "---sendLocationInfo===" + stompMessageEntity.toString());
        sendStompMsg(stompMessageEntity);
    }


    public static StompMessageEntity createStompMessage(String event) {
        return new StompMessageEntity(UUID.randomUUID().toString(), SpHelper.getInstance().getUserId(), event);
    }

    public static StompMessageEntity createAppStompMessage(String event) {
        return new StompMessageEntity(UUID.randomUUID().toString(), SpHelper.getInstance().getUserId(), event, Constant.APP_PERSON);
    }

    /** 返回一个拥有caseid 和 checkServiceName的类 */
    public static StompMessageEntity creatGetPointMessage(String event, String caseId){
        Lg.i(TAG, "---creatGetPointMessage===" + caseId);
        StompMessageEntity entity = new StompMessageEntity(UUID.randomUUID().toString(), SpHelper.getInstance().getUserId(), event);
        entity.getNeedBean(caseId,true);
        return entity;
    }

    /** 发送好友间聊天消息 */
    public static void SendFriendMessage(String roomId, ChatMsgTextEntity entity){
        EventBusMessage message = new EventBusMessage(SEND_MESSAGE_CODE);
        message.setFriendId(roomId);
        message.setChatMsgTextEntity(entity);
        EventBus.getDefault().post(message);
    }

}
